<template>
    <div class="app-container">
      <header-title/>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="留言内容" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入留言内容"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="留言类型" prop="type">
          <el-select
            v-model="queryParams.type"
            placeholder="请选择留言类型"
            clearable
          >
            <el-option
              v-for="dict in dict.type.feedback_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="留言时间">
          <el-date-picker
            style="width: 217px;"
            v-model="dateRange"
            size="small"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="list">
        <el-table-column label="留言时间" align="center" prop="createTime" width="170"/>
        <el-table-column label="留言类型" align="center" prop="type" :formatter="row=>selectDictLabel(dict.type.feedback_type,row.type) || '-'" width="120"/>
        <el-table-column label="昵称" align="center" prop="feedbackName" min-width="150px"/>
        <el-table-column label="联系电话" align="center" prop="feedbackPhone" min-width="150px"/>
        <el-table-column label="内容" align="center" min-width="200px">
          <template slot-scope="scope">
            <div>{{scope.row.content}}</div>
            <div v-if="scope.row.url">
              <el-image fit="cover" style="width: 50px; height: 50px;margin:8px 8px 0 0" v-for="(item,index) in JSON.parse(scope.row.url)" :key="index" :src="item.url" :preview-src-list="getPrivewImages(scope.row.url)" :initial-index="index"></el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" min-width="200px">
          <template slot-scope="scope">
            <div>{{scope.row.leaveStatus==1?'已处理':'待处理'}}</div>
            <div class="color-orange" v-if="scope.row.leaveStatus==1">回复内容：{{scope.row.leaveContent || '暂无内容'}}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-chat-line-round"
              @click="handleUpdate(scope.row)"
            >{{scope.row.leaveStatus==1?'修改回复':'回复'}}</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <el-dialog title="回复留言" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="回复内容" prop="leaveContent">
            <el-input v-model="form.leaveContent" type="textarea" placeholder="请输入回复内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="open=false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listFeedback, leaveFeedback, delFeedback } from "@/api/operateManage/feedback";
  
  export default {
    name: "Feedback",
    dicts: ['feedback_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          leaveContent: [
            { required: true, message: "回复内容不能为空", trigger: "blur" }
          ]
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          source: 1,
          type:'',
          keyword:''
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        this.loading = true;
        if(this.dateRange && this.dateRange.length){
          this.queryParams.startTime = this.dateRange[0]
          this.queryParams.endTime = this.dateRange[1]
        }else{
          this.queryParams.startTime = undefined
          this.queryParams.endTime = undefined
        }
        listFeedback(this.queryParams).then(response => {
            this.list = response.data.records;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.resetForm("form");
        this.form = JSON.parse(JSON.stringify(row));
        this.open = true;
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            leaveFeedback(this.form).then(response => {
              this.$modal.msgSuccess("回复用户留言成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除该用户留言？').then(function() {
          return delFeedback({id:row.id});
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除用户留言成功");
        }).catch(() => {});
      }
    }
  };
  </script>
  
  